package com.brillilab.starter.controller.aio.comments;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.comments.CommentsClassifyEnum;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.comments.CommentsAddVo;
import com.brillilab.domain.vo.comments.CommentsListVo;
import com.brillilab.domain.vo.comments.CommentsPagePaVo;
import com.brillilab.domain.vo.experiments.ExperimentCommentAddVo;
import com.brillilab.domain.vo.plans.PlanCommentAddVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.comments.ICommentsService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.logic.comments.CommentsLogic;
import com.brillilab.service.logic.experiments.ExperimentLogic;
import com.brillilab.service.logic.plans.PlanCommentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.date.DateUtil;

/**
 * 评论信息
 * 
 * @author
 * @date 2019/12/03
 */
@RestController
@RequestMapping(value = "/api/comments")
public class CommentsController {

	@Resource
	private ICommentsService commentsService;
	@Resource
	private CommentsLogic commentsLogic;
	@Resource
	private UsersLogic usersLogic;
	@Resource
	private ExperimentLogic experimentLogic;
	@Resource
	private PlanCommentLogic planCommentLogic;
	@Resource
	private IFileManageService fileManageService;

	/**
	 * 新增评论
	 * 
	 * @param comments
	 * @return
	 */
	@PostMapping("/add")
	public ResponseVo add(@RequestBody CommentsAddVo addVo, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		Long commentId = commentsLogic.addComments(userInfoVo.getLabId(), userInfoVo.getLabMemberId(), addVo);

		// 下一版发布删除
		if (CommentsClassifyEnum.EXPERIMENT.getValue().equals(addVo.getClassify())) {
			ExperimentCommentAddVo commentVo = new ExperimentCommentAddVo();
			commentVo.setExperimentId(addVo.getClassifyId());
			commentVo.setType(addVo.getType());
			commentVo.setContent(addVo.getContent());
			if (addVo.getFileId() != null) {
				FileManage fileManage = fileManageService.selectById(addVo.getFileId());
				fileManage.setId(null);
				fileManage.setReferDataId(0L);
				fileManage.setAttachType(OssFileEnum.CommentFile.getKey());
				FileManage file = fileManageService.insert(fileManage);
				commentVo.setCommentFileIds(new Long[] { file.getId() });
			}
			experimentLogic.addComment(commentVo, userInfoVo);

		} else if (CommentsClassifyEnum.PLAN.getValue().equals(addVo.getClassify())) {
			PlanCommentAddVo commentVo = new PlanCommentAddVo();
			commentVo.setPlanMemberId(addVo.getClassifyId());
			commentVo.setPlanDate(addVo.getPlanDate() != null ? addVo.getPlanDate() : new Date());
			commentVo.setContent(addVo.getContent());
			if (addVo.getFileId() != null) {
				FileManage fileManage = fileManageService.selectById(addVo.getFileId());
				fileManage.setId(null);
				fileManage.setReferDataId(0L);
				fileManage.setAttachType(OssFileEnum.PlanCommentFile.getKey());
				FileManage file = fileManageService.insert(fileManage);
				commentVo.setCommentFileIds(new Long[] { file.getId() });
			}
			planCommentLogic.addPlanComment(commentVo, userInfoVo);
		}
		return ResponseVo.successMap("commentId", commentId);
	}

	/**
	 * 刪除评论
	 * 
	 * @param commentId
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo delete(@RequestParam Long commentId) {
		commentsLogic.delComments(commentId);
		return ResponseVo.success();
	}

	/**
	 * 获取评论数量
	 * 
	 * @param classify   分类：1实验，2计划，3课题，4研究内容，5实验流
	 * @param classifyId 分类id( 1实验-实验ID, 2计划-计划人ID 3课题-课题ID, 4研究内容-研究内容ID,
	 *                   5实验流-实验流ID )
	 * @param type       评论类型:1分析后数据、2产出样品、3分组、4方法、0其他
	 * @param token
	 * @return
	 */
	@GetMapping("/num")
	public ResponseVo commentNum(@RequestParam Integer classify, @RequestParam Long classifyId, Integer type,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		int commentNum = commentsService.getCommentNum(classify, classifyId, type);
		return ResponseVo.successMap("commentNum", commentNum);
	}

	/**
	 * 获取评论列表
	 * 
	 * @param classify   分类：1实验，2计划，3课题，4研究内容，5实验流
	 * @param classifyId 分类id( 1实验-实验ID, 2计划-计划人ID 3课题-课题ID, 4研究内容-研究内容ID,5实验流-实验流ID
	 *                   )
	 * @param type       评论类型:null-全部，1分析后数据、2产出样品、3分组、4方法、0其他
	 * @param token
	 * @return
	 */
	@GetMapping("/list")
	public ResponseVo getCommentList(@RequestParam Integer classify, @RequestParam Long classifyId, Integer type,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<CommentsListVo> listVo = commentsLogic.getCommentList(userInfoVo.getLabMemberId(), classify, classifyId,
				type);
		return ResponseVo.success(listVo);
	}

	/**
	 * 获取评论分页列表
	 * 
	 * @param paVo
	 * @param token
	 * @return
	 */
	@PostMapping("/page")
	public ResponseVo getCommentPage(@RequestBody CommentsPagePaVo paVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		PageVo<CommentsListVo> page = commentsLogic.getCommentPage(userInfoVo.getLabMemberId(), paVo);
		return ResponseVo.success(page);
	}

	/**
	 * 获取计划评论列表
	 * 
	 * @param planMemberId 计划人成员ID
	 * @param startDate 开始时间
	 * @param endDate   结束时间
	 * @param token
	 * @return
	 */
	@GetMapping("/planList")
	public ResponseVo getPlanList(@RequestParam Long planMemberId, @RequestParam String startDate, @RequestParam String endDate,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<CommentsListVo> listVo = commentsLogic.getPlanList(userInfoVo.getLabMemberId(), planMemberId,  DateUtil.parse(startDate), DateUtil.parse(endDate));
		return ResponseVo.success(listVo);
	}
	
	/**
	 * 获取计划评论数量
	 * 
	 * @param token
	 * @return
	 */
	@GetMapping("/planNum")
	public ResponseVo planNum(@RequestParam Long planMemberId, @RequestParam String startDate, @RequestParam String endDate,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		int commentNum = commentsService.planNum(userInfoVo.getLabMemberId(), planMemberId,  DateUtil.parse(startDate), DateUtil.parse(endDate));
		return ResponseVo.successMap("commentNum", commentNum);
	}

}
